<?php
App::uses('AppController', 'Controller');
/**
 * Articles Controller
 *
 * @property Article $Article
 */
class ArticlesController extends AppController {
	
	public $uses = array('Article', 'ArticlesCategory');


/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Article->recursive = 0;
		$this->set('articles', $this->paginate());
	}

		
		public function admin_catalogue() {
		$this->Article->recursive = 0;
		$this->set('articles', $this->paginate());
	}
/**
 * view method
 *
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Article->id = $id;
		if (!$this->Article->exists()) {
			throw new NotFoundException(__('Invalid article'));
		}
		$this->set('article', $this->Article->read(null, $id));
	}

/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index() {
		$this->paginate = array(
			'Article' => array(
				'recursive' => -1,
				'fields' => array(
					'Article.id_article', 'Article.ref_article', 'Article.description_article', 'Article.tarif_brut',
					'categorie.description_categorie', 'groupe_devis.libelle_groupe_devis'
				),
				'conditions' => array(),
				'order' => array(),
				'joins' => array(
					array('type' => 'LEFT', 'alias' => 'categorie', 'table' => 'articles_categories',
							'conditions' => 'Article.id_categorie = categorie.id_categorie'),
					array('type' => 'LEFT', 'alias' => 'groupe_devis', 'table' => 'groupe_devis',
							'conditions' => 'Article.id_groupe_devis = groupe_devis.id_groupe_devis'),
				)
			)
		);
		$this->set('articles', $this->paginate());
	}
		
		

/**
 * admin_view method
 *
 * @param string $id
 * @return void
 */
	public function admin_view($id = null) {
		$this->loadModel('GroupeDevi');
		$this->Article->id = $id;
		if (!$this->Article->exists()) {
			throw new NotFoundException(__('Invalid article'));
		}
		$arrayRes = $this->Article->read('id_categorie', $id);
		$arrayGroupe = $this->Article->read('id_groupe_devis', $id);
		$numCat = $arrayRes['Article']['id_categorie'];
		$cat = $this->ArticlesCategory->findByIdCategorie($numCat);
		
		$groupedevis = $this->GroupeDevi->find('list', array(
			'conditions' => array('GroupeDevi.id_groupe_devis' => $arrayGroupe['Article']['id_groupe_devis'])));
		
		$this->set('article', $this->Article->read(null, $id));
		$this->set('cat', $cat['ArticlesCategory']['description_categorie'] );
		$this->set('groupedevis', $groupedevis[$arrayGroupe['Article']['id_groupe_devis']] );
	}

/**
 * admin_add method
 *
 * @return void
 */
	public function admin_add() {
		$this->loadModel('GroupeDevi');
		
		$categories = $this->ArticlesCategory->find('list', array('fields' => array('ArticlesCategory.id_categorie', 'ArticlesCategory.description_categorie')));
		$groupedevis = $this->GroupeDevi->find('list', array('fields' => array('GroupeDevi.id_groupe_devis', 'GroupeDevi.libelle_groupe_devis')));
				
		$this->set('categories', $categories );
		$this->set('groupedevis', $groupedevis ); 
		if ($this->request->is('post')) {
			$this->Article->create();
						
			if ($this->Article->save($this->request->data)) {
				$this->Session->setFlash(__('L\'article a été enregistré.'), 'flash_success');
				$this->redirect(array('action' => 'index'));
			} 
						else {
				$this->Session->setFlash(__('L\'article n\'a pas été enregistré. Merci de réessayer.'));
			}
		}
	}

/**
 * admin_edit method
 *
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		$this->Article->id = $id;
		if (!$this->Article->exists()) {
			throw new NotFoundException(__('Invalid article'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Article->save($this->request->data)) {
				$this->Session->setFlash(__('L\'article a été modifié.'), 'flash_success');
				$this->redirect(array('action' => 'index'));
			} 
						else {
				$this->Session->setFlash(__('L\'article n\'a pas été modifié. Merci de réessayer.'));
			}
		} else {
			$this->request->data = $this->Article->read(null, $id);
			$this->loadModel('GroupeDevi');
		
			$categories = $this->ArticlesCategory->find('list', array('fields' => array('ArticlesCategory.id_categorie', 'ArticlesCategory.description_categorie')));
			$groupedevis = $this->GroupeDevi->find('list', array('fields' => array('GroupeDevi.id_groupe_devis', 'GroupeDevi.libelle_groupe_devis')));

			$this->set('categories', $categories );
			$this->set('groupedevis', $groupedevis ); 
		}
	}

/**
 * admin_delete method
 *
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Article->id = $id;
		if (!$this->Article->exists()) {
			throw new NotFoundException(__('Invalid article'));
		}
		
		if ($this->Article->supprimer($id)) {
			$this->Session->setFlash(__('L\'article a été supprimé.'), 'flash_success');
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('L\'article n\'a pas été supprimé. Merci de réessayer.'));
		$this->redirect(array('action' => 'index'));
	}
}
		 